define(["jquery","dg/datagride.config"],function($,$config){
	var tpl = {};

	// 题型tpl的集合
	var questTypeInit = function(){
		var h1 = "", h2 = "";
		var questTypeArr = $config.getAllQuestType();
		for (var i=0; i < questTypeArr.length; i++) {
			var $quest = $config.getQuest(questTypeArr[i]);
			if( typeof $quest.tpl() == "string" ){  // 组题型
				h1 += $quest.tpl();
			}else if( typeof $quest.tpl() == "object" ){ // 子题型
				h1 += $quest.tpl().label();
				h2 += $quest.tpl().input();
			}
		}
		return {
			"head":h1,
			"content":h2
		};
	}

	/**
	 * head初始化
	 */
	tpl.headInit = function(direction){
		var html = "";
		if( direction == "left" ){
			html += tpl_table_left();
			html += tpl_head_left();
		}else if( direction == "top" ){
			html += tpl_table_top();
			html += tpl_head_top();
		}
		html += questTypeInit().head;
		return html;
	}

	/**
	 * content初始化
	 */
	tpl.contentInit = function(direction){
		var html = "";
		if( direction == "left" ){
			html += tpl_content_left();
		}else if( direction == "top" ){
			html += tpl_content_top();
		}
		html += questTypeInit().content;
		return html;
	}

	// 根据type判断加载题型
	var judgeTplByType = function(){
		var h1 = "", h2 = "", h3 = ""; // 组label、子label、子input
		var arr1 = [], arr2 = []; // 组题型、子题型
		var questTypeArr = $config.getAllQuestType();
		for (var i=0; i < questTypeArr.length; i++) {
			var $quest = $config.getQuest(questTypeArr[i]);
			if( typeof $quest.tpl() == "string" ){
				arr1.push( questTypeArr[i] );
			}else if( typeof $quest.tpl() == "object" ){
				arr2.push( questTypeArr[i] );
			}
		}
		for (var i = 0; i < arr1.length; i++) {
			if( i == 0 ){
				h1 += '{{if head.type == "'+ arr1[i] +'"}}';
			}else{
				h1 += '{{else if head.type == "'+ arr1[i] +'"}}';
			}
			h1 += '{{include "'+ "tpl_" + arr1[i] +'",head}}';
			if( i == arr1.length - 1 ) h1 += '{{/if}}';
		}
		for (var i = 0; i < arr2.length; i++) {
			if( i == 0 ){
				h2 += '{{if head.type == "'+ arr2[i] +'"}}';
				h3 += '{{if detail.type == "'+ arr2[i] +'"}}';
			}else{
				h2 += '{{else if head.type == "'+ arr2[i] +'"}}';
				h3 += '{{else if detail.type == "'+ arr2[i] +'"}}';
			}
			h2 += '{{include "'+ "tpl_" + arr2[i] + "_label" +'",head}}';
			h3 += '{{include "'+ "tpl_" + arr2[i] + "_input" +'",detail}}';
			if( i == arr2.length - 1 ) {
				h2 += '{{/if}}';
				h3 += '{{/if}}';
			}
		}
		return {
			"headHavechildren":h1,
			"headNochildren":h2,
			"content":h3
		};
	}

	/**
	 * 模板tpl top
	 */
	// 表 total top
	var tpl_table_top = function(){
		return 	'<script id="tpl_table_total" type="text/html">'+
				'	<div class="datagride flex-column">'+
				'		<div class="datagride-head datagride-head-top flex-row">'+
				'			{{include "tpl_table_head",heads}}'+
				'		</div>'+
				'		<div class="datagride-content datagride-content-top flex-column"></div>'+
				'	</div>'+
				'</script>';
	}
	// 表 head top
	var tpl_head_top = function(){
		var h = judgeTplByType();
		return 	'<script id="tpl_table_head" type="text/html">'+
				'	{{each children as head i}}'+
				'		{{if head.type}}'+ // 题型
				'			{{if head.children}}'+
				'				<div class="flex-column" style="flex:{{head.flexLeaf}}">'+
				'					<div class="cell" style="flex:{{head.flex}}">'+ h.headHavechildren +'</div>'+
				'					<div class="flex-row">'+
				'						{{include "tpl_table_head",head}}'+
				'					</div>'+
				'				</div>'+
				'			{{else}}'+
				'				<div class="cell" style="flex:{{head.flex}}">'+ h.headNochildren +'</div>'+
				'			{{/if}}'+
				'		{{else}}'+ // title
				'			<div class="cell" style="flex:{{head.flex}}"><div>{{head.label.value}}</div></div>'+
				'		{{/if}}'+
				'	{{/each}}'+
				'</script>';
	}
	// 表 content top
	var tpl_content_top = function(){
		var h = judgeTplByType();
		return 	'<script id="tpl_table_content" type="text/html">'+
				'	{{each contents as content i}}'+
				'		<div class="flex-row">'+
				'			<div class="cell"><div>{{content.label}}</div></div>'+
				'			{{each content.details as detail j}}'+
				'				<div class="cell">'+ h.content +'</div>'+
				'			{{/each}}'+
				'		</div>'+
				'	{{/each}}'+
				'</script>';
	}

	/**
	 * 模板tpl left
	 */
	// 表 total left
	var tpl_table_left = function(){
		return 	'<script id="tpl_table_total" type="text/html">'+
				'	<div class="datagride flex-row">'+
				'		<div class="datagride-head datagride-head-left">'+
				'			{{include "tpl_table_head",heads}}'+
				'		</div>'+
				'		<div class="datagride-content datagride-content-left flex-row"></div>'+
				'	</div>'+
				'</script>';
	}
	// 表头 head left
	var tpl_head_left = function(){
		var h = judgeTplByType();
		return 	'<script id="tpl_table_head" type="text/html">'+
				'	{{each children as head i}}'+
				'		{{if head.type}}'+ // 题型
				'			{{if head.children}}'+
				'				<div class="flex-row">'+
				'					<div class="cell">'+ h.headHavechildren +'</div>'+
				'					<div>'+
				'						{{include "tpl_table_head",head}}'+
				'					</div>'+
				'				</div>'+
				'			{{else}}'+
				'				<div class="cell">'+ h.headNochildren +'</div>'+
				'			{{/if}}'+
				'		{{else}}'+ // title
				'			<div class="cell">'+
				'				<div style="{{head.label.style}}">{{head.label.value}}</div>'+
				'			</div>'+
				'		{{/if}}'+
				'	{{/each}}'+
				'</script>';
	};
	// 表内容 content left
	var tpl_content_left = function(){
		var h = judgeTplByType();
		return 	'<script id="tpl_table_content" type="text/html">'+
				'	{{each contents as content i}}'+
				'		<div>'+
				'			<div class="cell"><div>{{content.label}}</div></div>'+
				'			{{each content.details as detail j}}'+
				'				<div class="cell">'+ h.content +'</div>'+
				'			{{/each}}'+
				'		</div>'+
				'	{{/each}}'+
				'</script>';
	};

	return tpl;
});